package com.gitee.dbswitch.admin.controller.converter;


import cn.hutool.extra.spring.SpringUtil;
import com.gitee.dbswitch.admin.dao.APIAssignmentConfigDAO;
import com.gitee.dbswitch.admin.dao.DatabaseConnectionDAO;
import com.gitee.dbswitch.admin.entity.APIAssignmentConfigEntity;
import com.gitee.dbswitch.admin.entity.AssignmentTaskEntity;
import com.gitee.dbswitch.admin.entity.DatabaseConnectionEntity;
import com.gitee.dbswitch.admin.model.response.APIAssignmentDetailResponse;
import com.gitee.dbswitch.admin.model.response.AssignmentDetailResponse;
import com.gitee.dbswitch.common.converter.AbstractConverter;

/**
 * api
 *
 * @author natural
 */
public class APIAssignmentDetailConverter extends
        AbstractConverter<AssignmentTaskEntity, APIAssignmentDetailResponse> {

    @Override
    public APIAssignmentDetailResponse convert(AssignmentTaskEntity assignmentTaskEntity) {
        DatabaseConnectionDAO databaseConnectionDAO = SpringUtil.getBean(DatabaseConnectionDAO.class);
        APIAssignmentConfigDAO apiAssignmentConfigDAO = SpringUtil.getBean(APIAssignmentConfigDAO.class);
        APIAssignmentConfigEntity taskConfig = apiAssignmentConfigDAO.getByAssignmentTaskId(
                assignmentTaskEntity.getId());
        DatabaseConnectionEntity dstConn = databaseConnectionDAO
                .getById(taskConfig.getTargetConnectionId());

        APIAssignmentDetailResponse.Configuration config = new APIAssignmentDetailResponse.Configuration();
        config.setApiName(taskConfig.getApiName());
        config.setApiURL(taskConfig.getApiURL());
        config.setRequestMethod(taskConfig.getRequestMethod());
        config.setRequestTime(taskConfig.getRequestTime());
        config.setSynchronizeType(taskConfig.getSynchronizeType());
        config.setTargetTable(taskConfig.getTargetTable());
        config.setTargetConnectionId(dstConn.getId());
        config.setTargetConnectionName(dstConn.getName());
        config.setTargetSchema(taskConfig.getTargetSchema());
        config.setTargetOnlyCreate(taskConfig.getTargetOnlyCreate());
        config.setTargetDropTable(taskConfig.getTargetDropTable());
        config.setTargetAutoIncrement(taskConfig.getTargetAutoIncrement());
        config.setTableNameCase(taskConfig.getTableNameCase());
        config.setColumnNameCase(taskConfig.getColumnNameCase());
        config.setTableNameMapper(taskConfig.getTableNameMap());
        config.setColumnNameMapper(taskConfig.getColumnNameMap());
        config.setBatchSize(taskConfig.getBatchSize());
        config.setChannelSize(taskConfig.getChannelSize());
        config.setTargetSyncOption(taskConfig.getTargetSyncOption());
        config.setBeforeSqlScripts(taskConfig.getBeforeSqlScripts());
        config.setAfterSqlScripts(taskConfig.getAfterSqlScripts());

        APIAssignmentDetailResponse detailResponse = new APIAssignmentDetailResponse();
        detailResponse.setId(assignmentTaskEntity.getId());
        detailResponse.setName(assignmentTaskEntity.getName());
        detailResponse.setDescription(assignmentTaskEntity.getDescription());
        detailResponse.setScheduleMode(assignmentTaskEntity.getScheduleMode());
        detailResponse.setCronExpression(assignmentTaskEntity.getCronExpression());
        detailResponse.setCreateTime(assignmentTaskEntity.getCreateTime());
        detailResponse.setUpdateTime(assignmentTaskEntity.getUpdateTime());
        detailResponse.setConfiguration(config);

        return detailResponse;
    }

}
